package com.le.tester.webpage.redis.redisAsMQ;

import com.le.tester.utils.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Schedules;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 描述：使用redis作为生产者
 */
@Controller
@RequestMapping("/")
public class RedisMqProductionController {
    private static final Logger LOG = LoggerFactory.getLogger(RedisMqProductionController.class);

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private RedisUtil redisUtil;

    @ResponseBody
    @RequestMapping("redisMQProduction")
    public Map<String, Object> redisMQProduction(@RequestBody Map<String, Object> paramMap) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            Collection<Object> values = paramMap.values();
            if (!CollectionUtils.isEmpty(values)) {
                for (Object next : values) {
                    //从右边向redis添加list元素
                    Long size = redisTemplate.opsForList().rightPush("redisMqList", next);
                    LOG.info("向[redisMqList]中推送信息 = [" + next + "], 消息总数为[" + size + "]条");
                }
            }
        } catch (Exception e) {
            LOG.info("生产消息异常:{}", e.getMessage());
            e.printStackTrace();
        }
        resultMap.put("msg", "success");
        return resultMap;

    }


}
